package com.milks.controller.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.milks.bean.Pager;
import com.milks.bean.ProductImage;
import com.milks.entity.Brand;
import com.milks.entity.Product;
import com.milks.entity.ProductCategory;
import com.milks.service.BrandService;
import com.milks.service.ProductCategoryService;
import com.milks.service.ProductService;
import com.milks.utils.UploadPicture;

@Controller
@RequestMapping("/admin/product")
public class ProductAdminController {
	
	@Resource
	private ProductService productService;
	@Resource
	private ProductCategoryService productCategoryService;
	@Resource
	private BrandService brandService;
	
	@RequestMapping(value="/",method=RequestMethod.GET)
	public String index(Model model,@ModelAttribute Pager pager) {
		if (pager==null) 
			 pager = new Pager();
		Pager pagerRe = productService.findByPager(pager);
		model.addAttribute("pager", pagerRe);
		return "admin/product_list";
	}
	
	@RequestMapping(value="/productList",method=RequestMethod.GET)
	public String productList(Model model,@ModelAttribute Pager pager) {
		if (pager==null) 
			 pager = new Pager();
		Pager pagerRe = productService.findByPager(pager);
		model.addAttribute("pager", pagerRe);
		return "admin/product_list";
	}
	
	@RequestMapping(value="/productAdd",method=RequestMethod.GET)
	public String addProduct(Model model) {
		List<ProductCategory> productCategory = productCategoryService.getAll();
		model.addAttribute("productCategory", productCategory);
		List<Brand> brand = brandService.getAll();
		model.addAttribute("brand", brand);
		return "admin/product_input";
	}
	
	@RequestMapping(value="/productAdd",method=RequestMethod.POST)
	public String addProduct(Model model,Product product,HttpServletRequest request,@ModelAttribute Pager pager) {

		product.setProductImageList(BusinessDeal.getProductImageList(request, "uploadPicPath"));
		product.setFreezeStore(0);
		product.setCreateDate(new Date());
		productService.save(product);
		if (pager==null) 
			 pager = new Pager();
		Pager pagerRe = productService.findByPager(pager);
		model.addAttribute("pager", pagerRe);
		return "admin/product_list";
	}
	
	@RequestMapping(value="/productUpdate",method=RequestMethod.GET)
	public String updateProduct(Model model,String id) {
		List<ProductCategory> productCategory = productCategoryService.getAll();
		model.addAttribute("productCategory", productCategory);
		List<Brand> brand = brandService.getAll();
		model.addAttribute("brand", brand);
		Product product = productService.load(id);
		List<ProductImage> productImages =  product.getProductImageList();
		model.addAttribute("productImages", productImages);
		model.addAttribute("product", product);
		//是更新页面还是添加页面的标志
		model.addAttribute("IsUpdate", true);
		return "admin/product_input";
	}
	
	@RequestMapping(value="/productUpdate",method=RequestMethod.POST)
	public String updateProduct(Model model,Product product,HttpServletRequest request,@ModelAttribute Pager pager) {
		
		Product productOld = productService.load(product.getId());
		
		if(product.getBrand()!=null)
			productOld.setBrand(product.getBrand());
		if (BusinessDeal.getProductImageList(request, "uploadPicPath")!=null) {
			productOld.setProductImageList(BusinessDeal.getProductImageList(request, "uploadPicPath"));
		}
		productOld.setIsBest(product.getIsBest());
		productOld.setIsHot(product.getIsHot());
		productOld.setIsMarketable(product.getIsMarketable());
		productOld.setIsNew(product.getIsNew());
		if(product.getDescription()!=null)
			productOld.setDescription(product.getDescription());
		productOld.setMarketPrice(product.getMarketPrice());
		productOld.setMetaDescription(product.getMetaDescription());
		productOld.setMetaKeywords(product.getMetaDescription());
		productOld.setPrice(product.getPrice());
		productOld.setMarketPrice(product.getMarketPrice());
		productOld.setWeight(product.getWeight());
		productOld.setStore(product.getStore());
		model.addAttribute("product", product);
		
		productService.update(productOld);
		
		if (pager==null) 
			 pager = new Pager();
		Pager pagerRe = productService.findByPager(pager);
		model.addAttribute("pager", pagerRe);
		List<ProductImage> productImages =  product.getProductImageList();
		model.addAttribute("productImages", productImages);
		return "admin/product_list";
	}
	
	@RequestMapping(value="/uploadPicture")
	@ResponseBody
	public List<String> uploadPicture(Model model,HttpServletRequest request, HttpServletResponse response) {
		return UploadPicture.uplaod(request, response);
	}
	
	@RequestMapping(value="/productDelete",method=RequestMethod.GET)
	@ResponseBody
	public HashMap<String, String> deleteProduct(Model model,String[] ids) {
		
		if (ids!=null) {
			for (int i = 0; i < ids.length; i++) {
				productService.delete(ids[i]);
			}
		}
		HashMap<String, String> dataHashMap = new HashMap<String, String>();
		dataHashMap.put("status", "success");
		dataHashMap.put("message", "删除商品成功");
		return dataHashMap;
	}
	
}
